package com.taiyi.renaissancemall.ui.main.first.memberBenefits;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.MemberBenefitsBean;
import com.taiyi.renaissancemall.ui.main.first.memberBenefits.editOrder.MemberEditOrderActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//会员福利商品列表页面
public class MemberBenefitsActivity extends BaseActivity<MemberBenefitsView, MemberBenefitsPresent> implements MemberBenefitsView, SpringView.OnFreshListener {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.iv_member_icon)
    ImageView ivMemberIcon;
    @BindView(R.id.tv_member_count)
    TextView tvMemberCount;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    @BindView(R.id.iv_member_img)
    ImageView ivMemberImg;
    @BindView(R.id.top)
    ConstraintLayout top;
    private int page = 1;
    private List<MemberBenefitsBean.ComlistBean> data = new ArrayList<>();
    private MemberBenefitsListAdapter memberBenefitsListAdapter;
    private PopupWindow mPopWindow;
    private String commodityid;
    private String salenum;
    private String cishu;
    private List<MemberBenefitsBean.ComlistBean> comlist;
    private String introimg;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_member_benefits;
    }

    @Override
    public MemberBenefitsPresent initPresenter() {
        return new MemberBenefitsPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        titleTv.setText(getString(R.string.memberBenefits));
        introimg = getIntent().getStringExtra("introimg");
        RequestOptions options = new RequestOptions().bitmapTransform(new RoundedCorners(30));
        Glide.with(this).load(introimg).apply(options).into(ivMemberImg);
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);
        setAdapter(data);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        presenter.getMemberShopList(page + "");
    }

    @Override
    public void initEvent() {

    }

    @OnClick({R.id.backIv, R.id.iv_member_icon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.iv_member_icon:
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        showProgress();
        presenter.getMemberShopList(page + "");
    }

    @Override
    public void onLoadmore() {
        page++;
        showProgress();
        presenter.getMemberShopList(page + "");
    }

    @Override
    public void getMemberBenefitsShopsSuccess(MemberBenefitsBean memberBenefitsBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        cishu = memberBenefitsBean.cishu;
        if ("-1".equals(cishu)) {
            tvMemberCount.setText(R.string.sorryUCant);
        } else {
            tvMemberCount.setText(memberBenefitsBean.cishu);
        }
        comlist = memberBenefitsBean.comlist;
        if (page == 1) {
            data.clear();
            if (comlist == null || comlist.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
        }
        data.addAll(comlist);
        if (memberBenefitsListAdapter != null) {
            memberBenefitsListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getMemberBenefitsShopsFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    private void setAdapter(List<MemberBenefitsBean.ComlistBean> comlistBeans) {
        memberBenefitsListAdapter = new MemberBenefitsListAdapter(this, R.layout.layout_member_item, comlistBeans);
        recyclerView.setLayoutManager(new GridLayoutManager(MemberBenefitsActivity.this, 2));
        recyclerView.setAdapter(memberBenefitsListAdapter);
    }

    @Subscribe
    public void getMemberBenefitsItem(MemberBenefitsEvent memberBenefitsEvent) {

        commodityid = memberBenefitsEvent.comlistBean.commodityid;
        salenum = memberBenefitsEvent.comlistBean.salenum + "";
        if (!"0".equals(cishu)
                && !"-1".equals(cishu)) {
            Intent intent = new Intent(MemberBenefitsActivity.this, MemberEditOrderActivity.class);
            intent.putExtra("commodityid", commodityid);
            intent.putExtra("salenum", salenum);
            startActivity(intent);
        } else {
            ToastUtils.showLongToast("暂无领取次数");
        }

//        View contentView = LayoutInflater.from(MemberBenefitsActivity.this).inflate(R.layout.layout_popup_member, null);
//        mPopWindow = new PopupWindow(contentView,
//                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
//        mPopWindow.setContentView(contentView);
//
//        ImageView imageView = contentView.findViewById(R.id.iv_member_buy_icon);
//        TextView tv_name = contentView.findViewById(R.id.tv_member_buy_name);
//        TextView tv_price = contentView.findViewById(R.id.tv_member_buy_price);
//        TextView tv_byNow = contentView.findViewById(R.id.tv_member_buy_buyNow);
//        TextView tv_addCar = contentView.findViewById(R.id.tv_member_buy_addCar);
//
//        Glide.with(MemberBenefitsActivity.this).load(memberBenefitsEvent.comlistBean.smallimgurl).into(imageView);
//        String sName = "";
//        if (memberBenefitsEvent.comlistBean.name.contains("\r\n")) {
//            sName = memberBenefitsEvent.comlistBean.name.replace("\r\n", "");
//        } else {
//            sName = memberBenefitsEvent.comlistBean.name;
//        }
//        tv_name.setText(sName);
//        tv_price.setText(memberBenefitsEvent.comlistBean.zhuanprice);
//        commodityid = memberBenefitsEvent.comlistBean.commodityid;
//        salenum = memberBenefitsEvent.comlistBean.salenum + "";
//        tv_byNow.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mPopWindow.dismiss();
//                if (!"0".equals(cishu)
//                        && !"-1".equals(cishu)) {
//                    Intent intent = new Intent(MemberBenefitsActivity.this, MemberEditOrderActivity.class);
//                    intent.putExtra("commodityid", commodityid);
//                    intent.putExtra("salenum", salenum);
//                    startActivity(intent);
//                } else {
//                    ToastUtils.showLongToast("暂无领取次数");
//                }
//            }
//        });
//        tv_addCar.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mPopWindow.dismiss();
//                if (!"0".equals(cishu)
//                        && !"-1".equals(cishu)) {
//                    //添加购物车
//                } else {
//                    ToastUtils.showLongToast("暂无领取次数");
//                }
//            }
//        });
//        View rootview = LayoutInflater.from(MemberBenefitsActivity.this).inflate(R.layout.activity_member_benefits, null);
//        mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
